package ua.kiev.tosik.price.modules.clients.gui.files.preview;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultTableHeaderCellRenderer;
import ua.kiev.tosik.price.Main;
import ua.kiev.tosik.price.conf.SkinType;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;
import ua.kiev.tosik.price.modules.clients.models.FilterAction;
import ua.kiev.tosik.price.modules.clients.models.PricesFilesPreviewTableModel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.util.Vector;

public class PricesFilesPreviewTable {

	private JXTable table;
	private JScrollPane scrollPane;
	private PricesFilesPreviewTableModel model;
	private PricesFilesPreviewPanel previewPanel;
	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui.files.preview");
	private ModuleConfig config;

	public PricesFilesPreviewTable(PricesFilesPreviewPanel previewPanel) {
		this.previewPanel = previewPanel;
		config = ModuleConfig.getInstance();
		model = new PricesFilesPreviewTableModel(new Vector<Object[]>());

		table = new JXTable(model);
		scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		table.setFillsViewportHeight(true);

		table.addHighlighter(HighlighterFactory.createSimpleStriping());
		table.setColumnControlVisible(true);
		table.setHorizontalScrollEnabled(true);
		table.packAll();

		table.setColumnSelectionAllowed(true);
		table.setRowSelectionAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getColumnModel().getSelectionModel().addListSelectionListener(new ColumnListener());
		initHeaderRender();
	}

	public void packAll() {
		table.packAll();
	}

	public JScrollPane getTable() {
		return scrollPane;
	}

	public PricesFilesPreviewTableModel getModel() {
		return model;
	}

	public void initHeaderRender() {
		for (int i = 0; i < table.getColumnCount(); i++) {
			if ( Main.skinType == SkinType.SYSTEM ) {
				table.getColumnModel().getColumn(i).setHeaderRenderer(new MyTableHeaderRenderSystem());
			} else {
				table.getColumnModel().getColumn(i).setHeaderRenderer(new MyTableHeaderRender());
			}
		}
	}

	public Object getHeaderObject(int column) {
		return table.getTableHeader().getColumnModel().getColumn(column).getHeaderValue();
	}

	public void setColumnName(int selectedColumn, Object title) {
		table.getTableHeader().getColumnModel().getColumn(selectedColumn).setHeaderValue(title);
		if ( Main.skinType == SkinType.SYSTEM ) {
			table.getTableHeader().getColumnModel().getColumn(selectedColumn).setHeaderRenderer(new MyTableHeaderRenderSystem());
		} else {
			table.getTableHeader().getColumnModel().getColumn(selectedColumn).setHeaderRenderer(new MyTableHeaderRender());
		}
		model.setColumnName(selectedColumn, title.toString());
	}

	private class ColumnListener implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			int selectedColumn = table.getSelectedColumn();
			previewPanel.openFilterPanel(selectedColumn);
		}
	}

	private class MyTableHeaderRender extends SubstanceDefaultTableHeaderCellRenderer {

		private JLabel label = null;

		private MyTableHeaderRender() {
			super();
			label = new JLabel();
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setOpaque(true);
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			// Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			if ( value != null ) {
				if ( value instanceof FilterAction ) {
					label.setIcon( ((FilterAction)value).getIcon() );
				}
				label.setText("" + value);
			} else {
				label.setText("");
			}
			return label;
		}

		@Override
		protected void setValue(Object value) {
			if (value != null) {
				label.setText("" + value);
			} else {
				label.setText("");
			}
		}
	}

	private class MyTableHeaderRenderSystem extends DefaultTableCellRenderer {

		private JLabel label = null;

		private MyTableHeaderRenderSystem() {
			super();
			label = new JLabel();
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setOpaque(true);
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			// JLabel component = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			if ( value != null ) {
				if ( value instanceof FilterAction ) {
					label.setIcon( ((FilterAction)value).getIcon() );
				}
				label.setText("" + value);
			} else {
				label.setText("");
			}
			// label.setForeground(table.getForeground());
			// label.setBackground(table.getBackground());
			label.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
			return label;
		}

		/*@Override
		protected void setValue(Object value) {
			if (value != null) {
				label.setText("" + value);
			} else {
				label.setText("");
			}
		}*/
	}

}
